package com.wanbangee.marksystem.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.wanbangee.marksystem.entities.StatisticsAverageRanking;
import com.wanbangee.marksystem.service.ClassScoreAnalyzeService;
import com.wanbangee.marksystem.service.SelectStatistisGradeReportServiceApi;
import com.wanbangee.marksystem.service.UserCenterService;
import com.wanbangee.marksystem.service.SettingClientService;

@RestController
public class ClassScoreAnalyzeController {

	@Autowired
	private UserCenterService userCenterService = null;  //注入用户中心向外提供数据的接口
	
	@Autowired
	private SettingClientService settingClientService = null; //注入阅卷流程向外提供数据的接口
	
	@Autowired
	private ClassScoreAnalyzeService classScoreAnalyzeService;  //注入班级分数分析的服务层
	
	@Autowired
	private SelectStatistisGradeReportServiceApi selectStatistisGradeReportServiceApi;
	
	/**
	 * 根据学校Id查询正常状态的年级信息
	 * @param schId
	 * @return
	 */
	@GetMapping("/gradeScoreAnalyze/selectGradeBySchId/{schId}")
	public Map<String, Object> selectGradeBySchId(@PathVariable("schId") Integer schId){
		
		return selectStatistisGradeReportServiceApi.selectGradeByschId(schId);
	}
	
	/**
	 * 根据年级ID查询该年级的所有正常状态班级信息
	 * @param graId
	 * @return
	 */
	@GetMapping("/classScoreAnalyze/selectClassByGraId/{graId}")
	public Map<String, Object> selectClassByGraId(@PathVariable("graId") Integer graId){
		
		//通过调用用户中心向外提供数据的接口获取该年级的所有正常状态班级信息
		return userCenterService.selectClassInfoByGraId(graId);
	}
	
	/**
	 * 根据所选班级查询此班级所有考试
	 * @param claId
	 * @return
	 */
	@GetMapping("/classScoreAnalyze/selectExamByClaId/{claId}")
	public Map<String, Object> selectExamByClaId(@PathVariable("claId") Integer claId){
		//通过调用阅卷流程向外提供数据的接口查询此班级所有考试
		return settingClientService.selectExamByClaId(claId);
	}
	
	
	/**
	 * 查询班级总分平均分(total_avg)/班级名称(cla_nm)/班主任(tea_id/tea_nm)/人数(capita)/平均分排名(rank)/排名变化(rankChange)
	 * @param graId 年级ID
	 * @param claId	班级ID
	 * @param examId	考试ID
	 * @return
	 */
	@GetMapping("/classScoreAnalyze/selectExamClassInfo")
	public Map<String, Object> selectExamClassInfo(Integer claId ,Integer examId){
		//用来存储提示信息，和状态码，数据
		Map<String, Object> responseMap = new HashMap<String, Object>();
		
		responseMap = classScoreAnalyzeService.selectExamClassInfo(claId, examId);
		if(responseMap == null || responseMap.size() == 0) {
			//查询返回的年级信息为null或者没有，返回状态码10013，学校还未设置年级
			responseMap.put("code", 10013);
			responseMap.put("msg", "未查询到该班级的考试信息");
		}else {
			//返回状态码1001，提示信息：接口成功
			responseMap.put("code", 1001);
			responseMap.put("msg", "接口成功");
		}
		
		return responseMap;
	}
	
	/**
	 * 查询当前班级当前考试所有科目的平均分，排名，较上次排名变化，优秀率，优秀人数，良好率，良好人数，及格率，及格人数，最高分最，低分，任课老师名称，学科名称
	 * @param claId
	 * @param examId
	 * @return
	 */
	@GetMapping("/classScoreAnalyze/selectClassSubInfo")
	public Map<String, Object> selectClassSubInfo(Integer claId ,Integer examId){
		//用来存储提示信息，和状态码，数据
		Map<String, Object> responseMap = new HashMap<String, Object>();
		
		//调用classScoreAnalyzeService中selectClassSubInfo方法查询当前班级当前考试所有科目信息
		List<StatisticsAverageRanking> sars = classScoreAnalyzeService.selectClassSubInfo(claId, examId);
		
		if(sars == null || sars.size() == 0) {  //查询失败
			responseMap.put("code", 10013);
			responseMap.put("msg", "未查询到该班级考试科目信息");
		}else {
			//将查询到的科目信息放入map中返回给前端
			responseMap.put("sars", sars);
			//返回状态码1001，提示信息：接口成功
			responseMap.put("code", 1001);
			responseMap.put("msg", "接口成功");
		}
		return responseMap;
	}
	
}
